Imports System.Data.SqlClient

Public Class cls_kilosemanal_

    Implements ICloneable

#Region "Variables ..."
    Private int_idkilosemanal As Integer = 0
    Private dbl_kilos As Double = 0
    Private dbl_importe As Double = 0
    Private dte_fecha As DateTime = New System.DateTime(Now.Year, 1, 1)

    Private col_Items As Collections.Generic.List(Of cls_kilosemanal_) = Nothing

    Public Overrides Function ToString() As String
        Return fecha.Date.ToShortDateString
    End Function
#End Region

#Region "Propiedades ..."
    Property idkilosemanal() As Integer
        Get
            idkilosemanal = int_idkilosemanal
        End Get
        Set(ByVal value As Integer)
            int_idkilosemanal = value
        End Set
    End Property
    Property kilos() As Double
        Get
            kilos = dbl_kilos
        End Get
        Set(ByVal value As Double)
            dbl_kilos = value
        End Set
    End Property
    Property importe() As Double
        Get
            importe = dbl_importe
        End Get
        Set(ByVal value As Double)
            dbl_importe = value
        End Set
    End Property
    Property fecha() As DateTime
        Get
            fecha = dte_fecha
        End Get
        Set(ByVal value As DateTime)
            dte_fecha = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_kilosemanal_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_kilosemanal_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_kilosemanal_)
        While obj_Reader.Read()
            Dim Elemento As New cls_kilosemanal_
            Elemento.int_idkilosemanal = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idkilosemanal")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("kilos"))) = False Then
                Elemento.dbl_kilos = (obj_Reader.GetValue(obj_Reader.GetOrdinal("kilos")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importe"))) = False Then
                Elemento.dbl_importe = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importe")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
                Elemento.dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idkilosemanal = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idkilosemanal")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("kilos"))) = False Then
            dbl_kilos = (obj_Reader.GetValue(obj_Reader.GetOrdinal("kilos")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importe"))) = False Then
            dbl_importe = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importe")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
            dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "kilosemanal__Nuevo"
                .Parameters.Add("@kilos", SqlDbType.Decimal).Value = dbl_kilos
                .Parameters.Add("@importe", SqlDbType.Decimal).Value = dbl_importe
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de kilosemanal_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "kilosemanal__Modificar"
                .Parameters.Add("@idkilosemanal", SqlDbType.Int).Value = int_idkilosemanal
                .Parameters.Add("@kilos", SqlDbType.Decimal).Value = dbl_kilos
                .Parameters.Add("@importe", SqlDbType.Decimal).Value = dbl_importe
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de kilosemanal_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idkilosemanal As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "kilosemanal__Eliminar"
                .Parameters.Add("@idkilosemanal", SqlDbType.Int).Value = idkilosemanal
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de kilosemanal_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idkilosemanal As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "kilosemanal__Buscar"
                .Parameters.Add("@idkilosemanal", SqlDbType.Int).Value = idkilosemanal
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de kilosemanal_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "kilosemanal__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de kilosemanal_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "kilosemanal__CargarTodos"
                '.Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de kilosemanal_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_kilosemanal_Clon As New cls_kilosemanal_
        obj_kilosemanal_Clon.int_idkilosemanal = Me.int_idkilosemanal
        obj_kilosemanal_Clon.dbl_kilos = Me.dbl_kilos
        obj_kilosemanal_Clon.dbl_importe = Me.dbl_importe
        obj_kilosemanal_Clon.dte_fecha = Me.dte_fecha
        obj_kilosemanal_Clon.col_Items = Me.col_Items
        Return obj_kilosemanal_Clon
    End Function

End Class
